<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\YiJingIntroductionSortStore;
use App\YiJingIntroduction;
use App\YiJingIntroductionSort;
use App\Http\Controllers\Controller;

class YiJingIntroductionSortController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return YiJingIntroductionSort::orderBy('id', 'desc')->get();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(YiJingIntroductionSortStore $request)
    {
        YiJingIntroductionSort::create($request->all());
        return response(['message' => '创建成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\YiJingIntroductionSort $yiJingIntroductionSort
     * @return \Illuminate\Http\Response
     */
    public function show(YiJingIntroductionSort $yiJingIntroductionSort)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\YiJingIntroductionSort $yiJingIntroductionSort
     * @return \Illuminate\Http\Response
     */
    public function edit(YiJingIntroductionSort $yiJingIntroductionSort)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\YiJingIntroductionSort $yiJingIntroductionSort
     * @return \Illuminate\Http\Response
     */
    public function update(YiJingIntroductionSortStore $request, YiJingIntroductionSort $yiJingIntroductionSort)
    {
        $yiJingIntroductionSort->update($request->only(['name']));
        return response(['message' => '修改成功']);

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\YiJingIntroductionSort $yiJingIntroductionSort
     * @return \Illuminate\Http\Response
     */
    public function destroy(YiJingIntroductionSort $yiJingIntroductionSort)
    {
        if (YiJingIntroduction::where('type', $yiJingIntroductionSort->id)->first()) {
            return response(['message' => '该分类下存在内容，不能删除'], 400);
        }
        $yiJingIntroductionSort->delete();
        return response(['message' => '删除成功']);
    }
}
